package zh.demo.webuser.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zh.demo.commons.utils.R;
import zh.demo.commons.utils.StringUtils;
import zh.demo.webuser.annotation.AuthIgnore;
import zh.demo.webuser.model.UserEntity;
import zh.demo.webuser.service.WebUserService;

import java.math.BigDecimal;
import java.util.Date;

/**
 * Created by zhangheng on 2017/12/1.
 */
@RestController
@RequestMapping("/api/")
public class UserAuthentication {

    @Autowired
    private WebUserService userService;

    @AuthIgnore
    @RequestMapping(value = "ifMember")
    public R ifMember(String uid){
        UserEntity user = userService.queryObject(Long.valueOf(uid));
        if(user!=null){
            String isMember = user.getIsAssociator();
            if("0".equals(isMember))
                return R.ok().put("msg","是会员");
            else
                return R.ok().put("code",1).put("msg","不是会员");
        }else{
            return R.ok().put("code",1).put("msg","用户不存在");
        }
    }

    @AuthIgnore
    @RequestMapping(value="userAuthentication")
    public R userAuthentication(String uid, String type){
        UserEntity user = userService.queryObject(Long.valueOf(uid));
        if(user!=null){
            if(type.equals("0")){//个人认证
                String pc = user.getPersonalCertification();
                if("0".equals(pc))
                    return R.ok().put("msg","个人已认证");
                else
                    return R.ok().put("code",1).put("msg","未认证个人");
            }else{
                String ec = user.getEnterpriseCertification();
                if("0".equals(ec))
                    return R.ok().put("msg","企业已认证");
                else
                    return R.ok().put("code",1).put("msg","未认证企业");
            }
        }else{
            return R.ok().put("code",1).put("msg","用户不存在");
        }
    }

    @AuthIgnore
    @RequestMapping(value="updateuser")
    public R updateUser(String uid, String imagePath, String realname, String nickname, String sex, String birthday, String IDType, String ID, String IDImage,
                        String mobile, String email, String freeMoney, String adMoney, String address, String companyName, String businessLicence,
                        String fax, String telephone, String status, String isVerify, String enterpriseCertification, String personalCertification,
                        String isAgent, String agentLevel, String isAssociator){
        if(StringUtils.isNullOrEmpty(uid))
            return R.ok().put("code",1).put("msg","用户编号不能为空");
        UserEntity user = new UserEntity();
        user.setUserId(Long.valueOf(uid));
        user.setImagePath(imagePath);
        user.setRealname(realname);
        user.setSex(sex);
        user.setBirthday(new Date(birthday));
        user.setIdType(IDType);
        user.setId(ID);user.setIdImage(IDImage);
        user.setMobile(mobile);
        user.setEmail(email);
        if(StringUtils.isNullOrEmpty(freeMoney)){

        }else
            user.setFreeMoney(new BigDecimal(Long.valueOf(freeMoney)));

        if(StringUtils.isNullOrEmpty(adMoney)){

        }else
            user.setAdMoney(new BigDecimal(Long.valueOf(adMoney)));
        user.setAddress(address);
        user.setCompanyName(companyName);
        user.setBusinessLicence(businessLicence);
        user.setFax(fax);
        user.setTelephone(telephone);
        user.setStatus(Integer.valueOf(status));
        user.setIsVerify(isVerify);
        user.setEnterpriseCertification(enterpriseCertification);
        user.setPersonalCertification(personalCertification);
        user.setIsAgent(isAgent);
        user.setAgentLevel(agentLevel);
        user.setIsAssociator(isAssociator);

        userService.update(user);
        return R.ok();
    }
}
